package Client.ui;
import java.util.HashMap;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


import javax.swing.DefaultListModel;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;

import Client.Controler.AppliManager;
import VWorld.Avatar;



public class AvatarsListWindow {

	private JFrame frame;
	private DefaultListModel modelLConnected;
	private AppliManager appliManager;
	private HashMap<String, Avatar> nameAvatar;

	/**
	 * Create the application.
	 */
	public AvatarsListWindow(AppliManager app) {
		this.appliManager = app;
		initialize();
		
		this.frame.setVisible(true);
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frame = new JFrame();
		frame.setBounds(100, 100, 221, 300);
		//frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.getContentPane().setLayout(null);
		frame.setTitle("Avatars list");
		
		JLabel lblAvatarsList = new JLabel("Avatars list:");
		lblAvatarsList.setBounds(10, 11, 169, 14);
		frame.getContentPane().add(lblAvatarsList);
		
		JList lusers = this.getLUsers();
		frame.getContentPane().add(lusers);
	}

	private JList getLUsers() {
		
		
		this.modelLConnected = new DefaultListModel();
		Avatar [] lconnected = this.appliManager.seeAllAvatars();
		this.nameAvatar = new HashMap<String, Avatar>();
		for(Avatar av : lconnected){
			this.nameAvatar.put(av.name, av);
			this.modelLConnected.addElement(av.name);
		}
		
		final JList lusers = new JList(this.modelLConnected);
		lusers.setBounds(10, 24, 185, 227);
		
		lusers.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e){
				Object ob[] = lusers.getSelectedValues();
				displayInfo(nameAvatar.get(ob[0]));
			}

			
		});
		
		return lusers;
	}

	private void displayInfo(Avatar avatar) {
		new CreateAvatarWindow(appliManager, avatar, false, true);
	}
}
